import const, remotes, config, logger


class Node(object):    
    def __init__(self, host_port, public_key):
        self.host_port = host_port
        self.public_key = public_key

class Crowd(object):
    __members = {}
    __waiting_list = {}
    __logged_user = {}   
    logger = logger.setup_logging("Crowd", config.LOG_LOGINS)
     
    def add_waiting_list(self, user, jondo):
        self.__waiting_list[user] = jondo
                
    def show_logged_users(self):
        for user, jondo in self.__logged_user.items():
            print user + "@" + jondo.host_port[0] + ":" + str(jondo.host_port[1])
            
    def show_waiting_members(self):
        for user, jondo in self.__waiting_list.items():
            print user + "@" + jondo.host_port[0] + ":" + str(jondo.host_port[1])
            
    def show_active_members(self):
        for user, jondo in self.__members.items():
            print user + "@" + jondo.host_port[0] + ":" + str(jondo.host_port[1])
                
    def get_active_members(self):
        members = []
        for user, jondo in self.__members.items():
            members.append(jondo)
        return members
    
    def join_commit(self):
        try:
            for user, jondo  in self.__waiting_list.items():
                self.__members[user] = jondo
                #self.__waiting_list.remove(node)            
            self.__waiting_list = {}
            remotes.EventPublisher().raise_event(const.EVENT_JOINCOMMIT, "Time to rebuild your paths!")
            return 1
        except:
            return 0
        
    def remove(self, host):
        for user, jondo in self.__logged_user.items():
            if jondo.host_port[0] == host:
                del self.__logged_user[user]
                if self.__members.has_key(user):
                    del self.__members[user]
                if self.__waiting_list.has_key(user):
                    del self.__waiting_list[user]
                    
    def check_in(self, login, jondo):
        """Check if the user on that jondo can join the crowd.
        
        Every host can have one active jondo, and every user 
        can login from only one jondo. Thus, user and jondos are
        mapped together  
        
        Keyword arguments:
        user -- current user login
        jondo -- the node which is using 

        """
        if login not in self.__logged_user:
            jondo_ip, jondo_port = jondo.host_port 
            for user, node in self.__logged_user.items():
                ip, port = node.host_port
                if ip == jondo_ip:
                    self.logger.debug("Host " + ip + " already has one active user")
                    return 0
            self.__logged_user[login] = jondo
            return 1
        else:
            self.logger.debug("User " + user + " is already logged in, check in rejected")
            return 0
        
        
        
